Wang Haihua
🍈 🍉🍊 🍋 🍌
插值是离散函数逼近的重要方法,利用它可通过函数在有限个点处的取值状况,估算出函数在其他点处的近似值。
早在6世纪,中国的刘焯已将等距二次插值用于天文计算。17世纪之后,I.牛顿,J.-L.拉格朗日分别讨论了等距和非等距的一般插值公式。在近代,插值法仍然是数据处理和编制函数表的常用工具,又是数值积分、数值微分、非线性方程求根和微分方程数值解法的重要基础,许多求解计算公式都是以插值为基础导出的。
现在已知数据
x = [1,2,3,4,5,6]
y = [300,500,800,1300,3000,5000]
绘制函数图像:
import matplotlib.pyplot as plt # 导入matplotlib库中的pyplot模块,并命名为plt
%matplotlib inline
x = [1,2,3,4,5,6]
y = [300,500,800,1300,3000,5000]
plt.scatter(x,y) # 绘制散点图
我们希望对x,y关系进行插值,scipy
中一维插值函数的调用方式为:
interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
其中:
x
: 自变量数据y
: 因变量数据kind
:插值类型,可选的插值类型包括:等。
接下来我们来看一下这几种插值形式的样子:
from scipy.interpolate import interp1d
func_linear = interp1d(x,y,'linear')
func_nearest = interp1d(x,y,'nearest')
func_nearest_up = interp1d(x,y,'nearest-up')
func_zero = interp1d(x,y,'zero')
func_quadratic = interp1d(x,y,'quadratic')
func_cubic = interp1d(x,y,'cubic')
import numpy as np
x1 = np.linspace(1,6,100)
y_linear = func_linear(x1)
y_nearest = func_nearest(x1)
y_nearest_up = func_nearest_up(x1)
y_zero = func_zero(x1)
y_quadratic = func_quadratic(x1)
y_cubic = func_cubic(x1)
plt.figure(figsize=(20,10))
plt.subplot(2,3,1)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_linear,label='linear',color='red')
plt.legend()
plt.subplot(2,3,2)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_nearest,label='nearest',color='red')
plt.legend()
plt.subplot(2,3,3)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_nearest_up,label='nearest_up',color='red')
plt.legend()
plt.subplot(2,3,4)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_zero,label='zero',color='red')
plt.legend()
plt.subplot(2,3,5)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_quadratic,label='quadratic',color='red')
plt.legend()
plt.subplot(2,3,6)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_cubic,label='cubic',color='red')
plt.legend()
plt.savefig('images/sci0202.png')
图像为:
import matplotlib.pyplot as plt # 导入matplotlib库中的pyplot模块,并命名为plt
%matplotlib inline
import numpy as np
x = [1,2,3,4,5,6]
y = [300,500,800,1300,3000,5000]
plt.scatter(x,y) # 绘制散点图
plt.savefig('images/sci0201.png')
#help(interp1d)
from scipy.interpolate import interp1d
func_linear = interp1d(x,y,'linear')
func_nearest = interp1d(x,y,'nearest')
func_nearest_up = interp1d(x,y,'nearest-up')
func_zero = interp1d(x,y,'zero')
func_quadratic = interp1d(x,y,'quadratic')
func_cubic = interp1d(x,y,'cubic')
import numpy as np
x1 = np.linspace(1,6,100)
y_linear = func_linear(x1)
y_nearest = func_nearest(x1)
y_nearest_up = func_nearest_up(x1)
y_zero = func_zero(x1)
y_quadratic = func_quadratic(x1)
y_cubic = func_cubic(x1)
plt.figure(figsize=(20,10))
plt.subplot(2,3,1)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_linear,label='linear',color='red')
plt.legend()
plt.subplot(2,3,2)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_nearest,label='nearest',color='red')
plt.legend()
plt.subplot(2,3,3)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_nearest_up,label='nearest_up',color='red')
plt.legend()
plt.subplot(2,3,4)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_zero,label='zero',color='red')
plt.legend()
plt.subplot(2,3,5)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_quadratic,label='quadratic',color='red')
plt.legend()
plt.subplot(2,3,6)
plt.scatter(x,y,label='raw data')
plt.plot(x1,y_cubic,label='cubic',color='red')
plt.legend()
plt.savefig('images/sci0202.png')